package com.icedevcloud.upms.controller.admin.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.icedevcloud.common.core.api.R;
import com.icedevcloud.common.core.log.IceLog;
import com.icedevcloud.common.core.page.PageResult;
import com.icedevcloud.upms.controller.admin.user.dto.*;
import com.icedevcloud.upms.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2023-08-12
 */
@Tag(name = "用户接口类")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    private final ISysUserService iSysUserService;

    @IceLog
    @Operation(summary = "获取用户信息接口")
    @PostMapping("/getUserInfo")
    public R<UserInfoResDto> getUserInfo() {
        UserInfoResDto resDto = iSysUserService.getUserInfo();
        return R.ok(resDto);
    }

    @IceLog
    @SaCheckPermission("upms:user:page")
    @Operation(summary = "用户列表接口")
    @PostMapping("/userPage")
    public R<PageResult<UserExtResDto>> userPage(@RequestBody UserPageReqDto reqDto) {
        PageResult<UserExtResDto> result = iSysUserService.userPage(reqDto);
        return R.ok(result);
    }

    @IceLog
    @Operation(summary = "用户名是否存在接口")
    @PostMapping("/userNameExist")
    public R<Boolean> userNameExist(String username) {
        Boolean result = iSysUserService.userNameExist(username);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:user:add")
    @Operation(summary = "添加用户接口")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody @Validated AddUserReqDto reqDto) {
        Boolean result = iSysUserService.add(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:user:update")
    @Operation(summary = "更新用户接口")
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody @Validated UpdateUserReqDto reqDto) {
        Boolean result = iSysUserService.update(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:user:delete")
    @Operation(summary = "删除用户接口")
    @DeleteMapping("/delete")
    public R<Boolean> delete(@NotNull(message = "ID不能为空") Long id) {
        Boolean result = iSysUserService.delete(id);
        return R.ok(result);
    }

}
